package com.itheima.redbaby.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.engine.LimitBuyEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.ImageUtil;
import com.itheima.redbaby.utils.PromptManager;

/**
 * 限时抢购商品页面
 * 
 * @author xun
 * 
 */
public class LimitBuyActivity extends BaseActivity {

	private ListView productList;
	private MyLimitBuyAdpter adapter;
	private List<Product> listData;
	private LimitBuyEngine impl;
   private  TextView  backTv;
	/* ==========在商品列表中需要的数据============= */
	private int productId; // 产品id
	private String productName; // 产品名字
	private float marketprice; // 产品市场价格
	private float price; // 产品会员价
	private String pic; // 产品图片地址
	private long lifetime; // 构面限时

	/* ======================= */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.limit_buy_activity);
		backTv = (TextView) findViewById(R.id.backTv);
		adapter = new MyLimitBuyAdpter();
		productList = (ListView) findViewById(R.id.productList);
		impl = DaoAndEngineFactory.getImpl(LimitBuyEngine.class);
		
		backTv.setOnClickListener( new  OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent_home = new Intent(LimitBuyActivity.this, HomeActivity.class);
				startActivity(intent_home);
				finish();
			}
		});
		filldata();
	}

	private void filldata() {
		new MyHttpTask() {
			protected void onPreExecute() {
				PromptManager.showProgressDialog(LimitBuyActivity.this);
			};

			/***
			 * 获取后台数据
			 */
			@Override
			protected Object doInBackground(Void... params) {

				List<Product> limitPList = impl.geLimitBuyPList();
				System.out.println(limitPList.toString());
				return limitPList;

			}

			/**
			 * 获取后台返回 的数据 并且跟新ui 获得的是 topic 对象
			 * 
			 * 更新 holder.textContent.setText("酷 ~宝贝  ~宝贝~ 裤" + "时间");
			 * holder.imgIcon.setImageResource(imgsid[position]);
			 */
			@Override
			protected void onPostExecute(Object result) {
				listData = (List<Product>) result;
				productList.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				PromptManager.closeProgressDialog();
			}

		}.executeHttp();

		/**
		 * listview 中的跳转事件 点击时候我们 跳转到商品列表页面
		 */
		productList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Product product = listData.get(position);
				int productId = product.getId();
				Intent intent = new Intent(LimitBuyActivity.this,
						ProductDetailActivity.class);
				intent.putExtra("pId", productId);
				startActivity(intent);
			}
		});

	}

	class MyLimitBuyAdpter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view;
			ViewHolder holder;
			if (convertView != null) {
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				view = View.inflate(LimitBuyActivity.this,
						R.layout.limit_buy_item, null);
				holder = new ViewHolder();
				holder.pro_icon = (ImageView) view.findViewById(R.id.pro_icon);
				holder.iv_limit_buy_arrow = (ImageView) view
						.findViewById(R.id.iv_limit_buy_arrow);

				holder.textClothesName = (TextView) view
						.findViewById(R.id.textClothesName);
				holder.tv_limit_buy_marketPrice = (TextView) view
						.findViewById(R.id.tv_limit_buy_marketPrice);
				holder.limitPrice = (TextView) view
						.findViewById(R.id.limitPrice);
				holder.textClothesPrice = (TextView) view
						.findViewById(R.id.textClothesPrice);
				holder.textLeftTime = (TextView) view
						.findViewById(R.id.textLeftTime);
				holder.tv_limit_buy_AddToCart = (TextView) view
						.findViewById(R.id.tv_limit_buy_AddToCart);

				view.setTag(holder);
			}

			Product product = listData.get(position);
			productId = product.getId();
			productName = product.getName();
			marketprice = product.getMarketprice();
			price = product.getPrice();
			pic = product.getPic();
			lifetime = product.getLefttime();

			ImageUtil.loadImageFromUrl(holder.pro_icon, pic);
			holder.textClothesName.setText(productName);
			holder.limitPrice.setText("限时特价   \t");
			holder.tv_limit_buy_marketPrice.setText(marketprice + "");
			holder.textClothesPrice.setText(price + "");
			holder.textLeftTime.setText("剩余时间" + lifetime);
			return view;
		}

	}

	static class ViewHolder {
		ImageView pro_icon; // 商品图片
		ImageView iv_limit_buy_arrow;// 右边箭头

		TextView textClothesName; // 商品名字
		TextView tv_limit_buy_marketPrice; // 抢购之前的价格
		TextView limitPrice;// 限时抢购的价格提示
		TextView textClothesPrice;// 限时抢购的价格
		TextView textLeftTime;// 限时时间还有多少
		TextView tv_limit_buy_AddToCart;// 立即抢购

	}

}
